package com.mtiiot.fstcpserver.sm;

import com.mtiiot.fstcpserver.base.BaseService;
import com.mtiiot.fstcpserver.utils.Constants;
import com.mtiiot.fscore.bean.MetadataInfo;
import com.mtiiot.fscore.bean.elasticsearch.metadata.SmMetadata;
import com.mtiiot.fscore.inter1.SmService1;
import com.mtiiot.fscore.utils.RequestType;
import com.mtiiot.fscore.utils.StringUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import smscenter.WsSendSmsStub;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 16-8-15.
 */
public class SmServiceImpl1 extends BaseService implements SmService1{
    private static final Logger logger= LogManager.getLogger(SmServiceImpl1.class);

    /**
     *
     * @param metadataInfo 元数据信息
     * @param phones 电话号码数组
     * @param content 短信内容
     * @param topic 短信主题
     * @return
     */
    @Override
    public boolean sendShortMessage(MetadataInfo metadataInfo, String[] phones, String content,String topic) {
        boolean result=false;
        try {
            checkMetadata(metadataInfo);//验证元数据
            if(StringUtil.isEmpty(topic)){
                topic= Constants.DEFAULT_TOPIC_UNKNOW;
            }

            content="【"+topic+"】"+content;

            //1.发送短信
            //将手机号码数组转化为字符串类型
            String phone="";
            for(int i=0;i<phones.length;i++){
                phone+=phones[i];
                phone+=",";
            }
            //去掉phone字符串的最后一个逗号
            phone = phone.substring(0, phone.length()-1);
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            WsSendSmsStub ws=new WsSendSmsStub();
            WsSendSmsStub.OutSendSms oss=new WsSendSmsStub.OutSendSms();
            oss.setStrPhone(phone);
            oss.setStrContent(content);
            oss.setSendDate(dateFormat1.format(new Date()));
            oss.setCallerid("call001");
            oss.setAccesskey("123456");
            oss.setSendid("xxx");
            oss.setSourcecode("xxx");
            ws.outSendSms(oss);
            result=true;

            //2.元数据入redis队列
            SmMetadata md=new SmMetadata();
            setMetadataBeanData(metadataInfo,md,RequestType.INSERT.getName(),content);
            md.setContent(content);
            md.setPhones(phone);
            metadataInQueue(Constants.TOPIC_METADATAINFO, Constants.TYPE_METADATA_SM, md);
            return result;
        } catch (Exception e) {
            //3.异常信息,元数据入redis队列
            handleException(Constants.MODULE_SM,"SmServiceImpl1","sendShortMessage",e,logger);
            return result;
        }
    }


    public static void main(String[] args){
        try {
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            WsSendSmsStub ws=new WsSendSmsStub();
            WsSendSmsStub.OutSendSms oss=new WsSendSmsStub.OutSendSms();
            oss.setStrPhone("18696167082");
            oss.setStrContent("【仪器平台】hello");
            oss.setSendDate(dateFormat1.format(new Date()));
            oss.setCallerid("call001");
            oss.setAccesskey("123456");
            oss.setSendid("xxxx");
            oss.setSourcecode("xxxx");
            ws.outSendSms(oss);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
